package com.example.understand_compose1.fet_mapsdk

import android.os.Bundle
import android.preference.PreferenceManager
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.example.understand_compose1.R
import org.osmdroid.config.Configuration.*
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
import org.osmdroid.tileprovider.tilesource.XYTileSource
import org.osmdroid.util.GeoPoint
import org.osmdroid.util.MapTileIndex
import org.osmdroid.views.CustomZoomButtonsController
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.ScaleBarOverlay
import org.osmdroid.views.overlay.compass.CompassOverlay
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay


class FetOsmdoridActivity : AppCompatActivity() {

    private var mapView: MapView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this))
        enableEdgeToEdge()
        setContentView(R.layout.activity_fet_osmdorid)


        mapView = findViewById<MapView>(R.id.map)
        mapView?.setTileSource(TileSourceFactory.MAPNIK)

        mapView?.maxZoomLevel = 23.0
        mapView?.minZoomLevel = 0.0
        mapView?.controller?.setZoom(12.0)
        //让瓦片适应不同像素密度:默认地图显示的字体小，图片像素高，可设置以下代码，使地图适应不同像素密度，更美观
        mapView?.isTilesScaledToDpi = true
        //设置缩放按钮可见
        val zoomController = mapView?.getZoomController()
        zoomController?.setVisibility(CustomZoomButtonsController.Visibility.NEVER)
        mapView?.setMultiTouchControls(true) // 触控放大缩小
        mapView?.overlayManager?.tilesOverlay?.isEnabled = true
        mapView?.isSelected = true
        var dm = resources.displayMetrics
        //指南针
        var mCompassOverlay = CompassOverlay(
            this, InternalCompassOrientationProvider(this),
            mapView
        )
        mCompassOverlay.enableCompass()
        mapView?.getOverlays()?.add(mCompassOverlay)
        //比例尺配置
        var mScaleBarOverlay = ScaleBarOverlay(mapView)
        mScaleBarOverlay.setScaleBarOffset(dm.widthPixels / 2, 10)
        mapView?.getOverlays()?.add(mScaleBarOverlay)
        mapView?.overlays?.add(mScaleBarOverlay)
        //定位
        var mLocationOverlay = MyLocationNewOverlay(GpsMyLocationProvider(this), mapView)
        mapView?.overlays?.add(mLocationOverlay)
        mLocationOverlay.enableMyLocation()
        //地图移动到该点
        var startPoint = GeoPoint(GeoPoint(34.360284, 108.859602))
        mapView?.controller?.setCenter(startPoint)

        mapView?.setUseDataConnection(true)

        val wz =
            "tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&tk=自己申请的token"

        //影像地图 _W是墨卡托投影  _c是国家2000的坐标系
        val tianDiTuImgTileSource: OnlineTileSourceBase = object : XYTileSource(
            "Tian Di Tu Img", 1, 18, 256, "",
            arrayOf(
                "https://t0.$wz",
                "https://t1.$wz",
                "https://t2.$wz",
                "https://t3.$wz",
                "https://t4.$wz",
                "https://t5.$wz",
                "https://t6.$wz",
                "https://t7.$wz"
            )
        ) {
            override fun getTileURLString(pMapTileIndex: Long): String {
                Log.d(
                    "url",
                    (baseUrl + "&TILEROW=" + MapTileIndex.getY(pMapTileIndex) + "&TILECOL=" + MapTileIndex.getX(
                        pMapTileIndex
                    )
                            + "&TILEMATRIX=" + MapTileIndex.getZoom(pMapTileIndex))
                )
                return (baseUrl + "&TILEROW=" + MapTileIndex.getY(pMapTileIndex) + "&TILECOL=" + MapTileIndex.getX(
                    pMapTileIndex
                )
                        + "&TILEMATRIX=" + MapTileIndex.getZoom(pMapTileIndex))
            }
        }


        mapView?.setTileSource(tianDiTuImgTileSource)


    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        val permissionsToRequest = ArrayList<String>()
        var i = 0
        while (i < grantResults.size) {
            permissionsToRequest.add(permissions[i])
            i++
        }
        if (permissionsToRequest.size > 0) {
            ActivityCompat.requestPermissions(
                this,
                permissionsToRequest.toTypedArray(),
                1)
        }
    }
}